#include <config.h>
#include <input_manager.h>
#include <string.h>

static PT_InputOpr g_ptInputOprHead;
static pthread_mutex_t g_tMutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t g_tCondvar = PTHREAD_COND_INITIALIZER; 
static T_InputEvent g_tInputEvent;

int RegisterInputOpr(PT_InputOpr ptInputOpr)
{
	PT_InputOpr ptTmp;

	if(!g_ptInputOprHead)
	{
		g_ptInputOprHead = ptInputOpr;
		ptInputOpr->ptNext = NULL;
	}
	else
	{
		ptTmp = g_ptInputOprHead;
		while(ptTmp->ptNext)
		{
			ptTmp = ptTmp->ptNext;
		}
		ptTmp->ptNext = ptInputOpr;
		ptInputOpr->ptNext = NULL;
	}
	return 0;
}


void ShowInputOpr(void)
{
	PT_InputOpr ptTmp;

	ptTmp = g_ptInputOprHead;
	while(ptTmp)
	{
		printf("%s \n", ptTmp->name);
		ptTmp = ptTmp->ptNext;
	}
}

static void *InputEventThreadFunction(void *pVoid)
{
	T_InputEvent tInputEvent;
	
	int (*GetInputEvent)(PT_InputEvent ptInputEvent);
	GetInputEvent = (int (*)(PT_InputEvent))pVoid;
	
	while(1)
	{		
		if(0 == GetInputEvent(&tInputEvent))
		{		
			pthread_mutex_lock(&g_tMutex);
			
			g_tInputEvent = tInputEvent;

			pthread_cond_signal(&g_tCondvar);

			pthread_mutex_unlock(&g_tMutex);
		}
	}
	return NULL;
}

int AllInputDeviceInit(void)
{
	PT_InputOpr ptTmp = g_ptInputOprHead;
	int iError = -1;

	while(ptTmp)
	{
		if(ptTmp->DeviceInit() == 0)
		{
			pthread_create(&ptTmp->tThreadID, NULL, InputEventThreadFunction, ptTmp->GetInputEvent);
			iError = 0;
		}
		ptTmp = ptTmp->ptNext;
	}
	return iError;
}

int GetInputEvent(PT_InputEvent ptInputEvent)
{
	pthread_mutex_lock(&g_tMutex);
	pthread_cond_wait(&g_tCondvar, &g_tMutex);

	*ptInputEvent = g_tInputEvent;
	pthread_mutex_unlock(&g_tMutex);

	return 0;
}

int InputInit(void)
{
	int iError;

	iError = TouchscreenRegister();
	iError |= StdinRegister();
	
	return iError;
}

